package com.itly.controller;

import com.itly.util.ApiResult;
import com.itly.entity.Teacher;
import com.itly.service.TeacherService;
import com.itly.util.ApiResultHandler;
import com.itly.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TeacherController {
    private Logger logger = LoggerFactory.getLogger(TeacherController.class);

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/teachers/{page}/{size}")
    public ApiResult teachers(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageBean<Teacher> teacherPageInfo = teacherService.selectTeacherByPage(page, size);
        return ApiResultHandler.buildApiResult(200, "查询教师", teacherPageInfo);
    }

    @PostMapping("/teacher")
    public ApiResult addTeacher(@RequestBody Teacher teacher) {
        Integer integer = teacherService.addTeacher(teacher);
        if (integer == 1) {
            logger.info("教师添加成功");
            return ApiResultHandler.buildApiResult(200, "增加成功", integer);
        } else {
            logger.info("教师添加失败",teacher);
            return ApiResultHandler.buildApiResult(400, "增加失败", integer);
        }
    }

    @GetMapping("/teacher/{id}")
    public ApiResult teacher(@PathVariable("id") Integer id) {
        Teacher teacher = teacherService.selectTeacherById(id);
        return ApiResultHandler.buildApiResult(200, "查询教师", teacher);
    }

    @PutMapping("/teacher")
    public ApiResult updateTeacher(@RequestBody Teacher teacher) {
        Integer integer = teacherService.updateTeacher(teacher);
        if (integer == 1) {
            logger.info("教师信息更新成功");
            return ApiResultHandler.buildApiResult(200, "更新成功", integer);
        } else {
            logger.info("教师信息更新失败",teacher);
            return ApiResultHandler.buildApiResult(400, "更新失败", integer);
        }
    }

    @DeleteMapping("/teacher/{id}")
    public ApiResult deleteTeacher(@PathVariable("id") Integer id) {
        Integer integer = teacherService.deleteTeacher(id);
        if (integer == 1) {
            logger.info("教师删除成功");
            return ApiResultHandler.buildApiResult(200, "删除成功", integer);
        } else {
            logger.info("教师删除失败",id);
            return ApiResultHandler.buildApiResult(400, "删除失败", integer);
        }
    }

    @PostMapping("/admin_teacherExit")
    public ApiResult exit(HttpServletRequest request) {
        logger.info("退出登录。。。");
        String admin = (String) request.getSession().getAttribute("admin");
        String teacher = (String) request.getSession().getAttribute("teacher");
        if (admin != null) {
            request.getSession().removeAttribute("admin");
        }
        if (teacher != null) {
            request.getSession().removeAttribute("teacher");
        }
        logger.info("退出登录成功");
        return ApiResultHandler.buildApiResult(200, "退出成功", null);
    }

    /**
     * 修改密码
     *
     * @param teacher
     * @return
     */
    @PutMapping("/teacherPWD")
    public ApiResult updatePwd(@RequestBody Teacher teacher) {
        logger.info("教师修改密码");
        teacherService.updatePwdTeacher(teacher);
        return ApiResultHandler.buildApiResult(200, "密码更新成功", null);
    }
}
